﻿using Abp.Domain.Entities;
using Abp.EntityFramework;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Basefr.Data.EntityFrameworkCore
{
    public class DbContextEntityFinder : IDbContextEntityFinder
    {
        private readonly IServiceProvider _serviceProvider;

        public DbContextEntityFinder(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
        }

        public IEnumerable<EntityTypeInfo> GetEntityTypeInfos(Type dbContextType)
        {
            var dbContext = _serviceProvider.GetService(dbContextType);
            var entityTypes = ((DbContext)dbContext).Model.GetEntityTypes().ToList();

            return entityTypes.Select(t => new EntityTypeInfo(t.ClrType, dbContextType)).ToList();
        }
    }
}
